using System;
using System.Collections.Generic;
using System.Text;

namespace jsonReader 
{
    public class JsonArray : IJsonValue 
    {
        private string name;
        private JsonElementCollection jsonElements;

        public JsonArray(string name, JsonElementCollection elements) {
            if (string.IsNullOrEmpty(name)) { 
                throw new ArgumentNullException("name");
            }

            this.name = name;
            this.jsonElements = elements;
        }

        public string Name {
            get { return name; }
            set { name = value; }
        }

        public JsonElementCollection JsonElements {
            get {
                return jsonElements;
            }
        }

        #region IJsonValue Members
        public string ToJsonString() {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");

            foreach (IJsonValue element in this.jsonElements) {
                sb.AppendFormat("   {0}", element).AppendLine(", ");
            }

            if (4 < sb.Length) {
                sb.Remove(sb.Length - 4, 4);
            }

            sb.AppendLine("]");

            return sb.ToString();
        }

        #endregion

        /// <returns>A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.</returns>
        public override string ToString() {
            return ToJsonString();
        }
    }
}
